package com.gxar.marketdelivery.domain.validator;

import com.gxar.marketdelivery.domain.repo.DeliveryResSlotRepository;
import com.gxar.marketdelivery.infra.dao.entity.DeliveryResSlotEntity;
import com.gxar.marketdelivery.interfaces.web.result.IResponseStatusMsg;
import com.gxar.marketdelivery.shared.model.DeliveryPlanModel;
import com.gxar.marketdelivery.shared.utils.AssertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author nice
 * @version 1.0
 * @created 2023/7/23.
 */
@Component
public class DeliveryPlanValidator {
    @Autowired
    protected DeliveryResSlotRepository deliveryResSlotRepository;

    private DeliveryResSlotEntity getAndCheckSlot(Long resSlotId) {
        DeliveryResSlotEntity entity = deliveryResSlotRepository.getById(resSlotId);
        AssertUtils.notNull(entity, IResponseStatusMsg.APIEnum.PARAM_ERROR, "资源位不存在");
        return entity;
    }

    public void validatePlans(List<DeliveryPlanModel> planModels) {
        planModels.forEach(this::validatePlan);
    }

    public void validatePlan(DeliveryPlanModel planModel) {
        DeliveryResSlotEntity resSlotEntity = getAndCheckSlot(planModel.getResSlotId());
        AssertUtils.notNull(planModel.getMaterialModel(), IResponseStatusMsg.APIEnum.PARAM_ERROR, "投放物料不能为空");
        SchemaValidator.validateMaterial(resSlotEntity.getPlanStaticSchema(), planModel.getMaterialModel());
    }
}
